package tm.testing;

import java.util.ArrayList;
import java.util.NoSuchElementException;

import com.jme.renderer.ColorRGBA;
import com.jme.renderer.Renderer;
import com.jme.scene.Spatial;
import com.jme.scene.state.MaterialState;

public class ColorOverlay {
	
	private ArrayList<Spatial> objects = new ArrayList<Spatial>();
	private MaterialState localMaterialState;
	
	public ColorOverlay(ColorRGBA color, Renderer rend, boolean enabled) {
		localMaterialState=rend.createMaterialState();
        localMaterialState.setAmbient(color.clone());
        localMaterialState.setDiffuse(color.clone());
        localMaterialState.setEnabled(enabled);
	}
	
	public void addSpatial(Spatial s) {
		objects.add(s);
		objects.get(objects.size()-1).setRenderState(localMaterialState);
		objects.get(objects.size()-1).updateRenderState();
	}
	
	public void removeSpatial(Spatial s) {
		if(objects.contains(s)) {
			int index = objects.indexOf(s);
			objects.get(index).clearRenderState(localMaterialState.getStateType());
			objects.get(index).updateRenderState();
			objects.remove(index);
		} else {
			throw new NoSuchElementException("The given Spatial is not in the list of this ColorOverlay object.");
		}
	}
	
	public boolean isContained(Spatial s) {
		if(objects.contains(s)) {
			return true;
		} else {
			return false;
		}
	}
	
	public void setEnabled(boolean b) {
		localMaterialState.setEnabled(b);
	}
	
}